package com.liushuang.waiwaiim.model;

import android.content.Context;

import com.liushuang.waiwaiim.model.bean.UserInfo;
import com.liushuang.waiwaiim.model.dao.UserAccountDao;
import com.liushuang.waiwaiim.model.db.DBManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Model {

    private static volatile Model instance;
    private Context mContext;
    private ExecutorService executorService = Executors.newCachedThreadPool();
    private UserAccountDao mUserAccountDao;
    private DBManager dbManager;
    private EventListener mEventListener;

    private Model() {}

    public static Model getInstance() {
        if (instance == null) {
            synchronized (Model.class) {
                if (instance == null) {
                    instance = new Model();
                }
            }
        }
        return instance;
    }

    public void init(Context context){
        mContext = context;
        mUserAccountDao = new UserAccountDao(mContext);

        mEventListener = new EventListener(mContext);
    }

    public ExecutorService getGlobalExecutorService(){
        return executorService;
    }

    public void loginSuccess(UserInfo userInfo) {

        if (userInfo == null){
            return;
        }

        if (dbManager != null){
            dbManager.close();
        }

        dbManager = new DBManager(mContext, userInfo.getName());

    }

    public DBManager getDbManager(){
        return dbManager;
    }

    public UserAccountDao getUserAccountDao() {
        return mUserAccountDao;
    }
}
